Page({
    data: {
      name: "", // ✅ 孩子姓名
      idCard: "", // ✅ 身份证号
      photo: "", // ✅ 照片
      sex: 0, // ✅ 性别：0-男，1-女
      age: "", // ✅ 年龄
      school: "", // ✅ 学校
      schoolClass: "", // ✅ 班级
      phone: "", // ✅ 电话
      teacherName: "", // ✅ 老师姓名
      teacherPhone: "" // ✅ 老师电话
    },
  
    onInputChange(e) {
      const { field } = e.currentTarget.dataset;
      this.setData({ [field]: e.detail.value });
    },
  
    onGenderChange(e) {
      this.setData({ sex: e.detail.value }); // ✅ 修改性别字段
    },
  
    uploadPhoto() {
      wx.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
          console.log("📌 选择的照片:", res.tempFilePaths[0]);
          this.setData({ photo: res.tempFilePaths[0] });
        }
      });
    },
  
    bindChild() {
      console.log("提交绑定信息:", this.data);
  
      if (!this.data.name || !this.data.idCard || !this.data.age || !this.data.school || !this.data.schoolClass) {
        wx.showToast({ title: '请填写完整信息', icon: 'none' });
        return;
      }
  
      wx.request({
        url: 'http://localhost:8081/parent/child/bind',
        method: 'POST',
        data: {
          name: this.data.name,
          idCard: this.data.idCard,
          photo: this.data.photo,
          sex: Number(this.data.sex),
          age: Number(this.data.age),
          school: this.data.school,
          schoolClass: this.data.schoolClass,
          phone: this.data.phone,
          teacherName: this.data.teacherName,
          teacherPhone: this.data.teacherPhone
        },
        header: {
          'Authorization': `Bearer ${wx.getStorageSync('token')}`,
          'parentId': wx.getStorageSync('parentId'), // ✅ 传递 `parentId` 作为 Header
          'content-type': 'application/json'
        },
        success: (res) => {
          console.log("绑定成功:", res);
          if (res.statusCode === 200) {
            wx.showToast({ title: '绑定成功', icon: 'success' });
            wx.navigateBack();
          } else {
            wx.showToast({ title: res.data || '绑定失败', icon: 'none' });
          }
        },
        fail: (err) => {
          console.error("绑定失败:", err);
          wx.showToast({ title: '绑定失败，请重试', icon: 'none' });
        }
      });
    }
  });
  